import { NameSpace } from 'utils';
import exceed from 'utils/apimap';
import { updatePageInfo } from 'pages/index/actions';

const ns = NameSpace('project');
export const UPDATE_COMMITS_PAGE = ns('UPDATE_COMMITS_PAGE');
export const RECEIVE_DATA = ns('RECEIVE_DATA');
export const RECEIVE_PROJECTINFO = ns('RECEIVE_PROJECTINFO');
export const PROJECT_RECEIVE_LAST_MIRROR_RECORD = ns('PROJECT_RECEIVE_LAST_MIRROR_RECORD');
export const RESET_DATA = ns('RESET_DATA');
export const RESET_ISPROJECTINFOFETCHED = ns('RESET_ISPROJECTINFOFETCHED');
export const SET_IMPORT_STATUS = ns('SET_IMPORT_STATUS');
export const UPDATE_DATA = ns('UPDATE_DATA');
export const PROJECT_RECEIVE_MiRROR_SETTINGS = ns('PROJECT_RECEIVE_MiRROR_SETTINGS');

export const CODEUP_GUIDE_FLOW = ns('CODEUP_GUIDE_FLOW');
export const CODEUP_GUIDE_SAFETY = ns('CODEUP_GUIDE_SAFETY');
export const CODEUP_GUIDE_MERGE = ns('CODEUP_GUIDE_MERGE');

export const CODEUP_GEN_WATERMARK_BASE64 = ns('CODEUP_GEN_WATERMARK_BASE64');

export function updateCommitsPage(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_COMMITS_PAGE,
      data,
    });
  };
}

export function updateData(data) {
  return (dispatch) => {
    dispatch({
      type: UPDATE_DATA,
      data,
    });
  };
}

export function getPublicData() {
  return (dispatch) => {
    exceed
      .forceFetch({
        api: 'demo.get',
      })
      .then((result) => {
        dispatch({
          type: RECEIVE_DATA,
          data: result,
        });
      });
  };
}

export function resetData() {
  return (dispatch) => {
    dispatch({
      type: RESET_DATA,
    });
  };
}

// 手动设置立即进入导入中
export function setImportStatus(data) {
  return (dispatch) => {
    dispatch({
      type: SET_IMPORT_STATUS,
      data,
    });
  };
}

export function resetIsProjectInfoFetched() {
  return (dispatch) => {
    dispatch({
      type: RESET_ISPROJECTINFOFETCHED,
    });
  };
}

/**
 * 获取库的基本信息
 * @param {*} cb 回调函数
 * @param {*} projectDidMount 首次进入库内
 */
export const getProjectInfo = function (cb) {
  return (dispatch, getState) => {
    const pageInfo = (getState().root && getState().root.pageInfo) || {};
    const pathResource = pageInfo.pathResource || {};
    exceed
      .fetch({
        api: 'projects.single.get',
        params: {
          id: pathResource.id,
        },
      })
      .then((result) => {
        window.FORCE.pathResource.projectInfo = result;
        dispatch(
          updatePageInfo(window.location.pathname, () => {
            dispatch({
              type: RECEIVE_PROJECTINFO,
              data: result,
            });
            cb && cb(result);
          }),
        );
      })
      .fail((err) => {
        console.error(err);
        cb && cb();
      });
  };
};

// codeup 引导 流水线
export const codeupGuideFlowActive = function (data) {
  return (dispatch) => {
    dispatch({
      type: CODEUP_GUIDE_FLOW,
      data,
    });
  };
};
// codeup 引导 安全
export const codeupGuideSafetyActive = function (data) {
  return (dispatch) => {
    dispatch({
      type: CODEUP_GUIDE_SAFETY,
      data,
    });
  };
};
// codeup 引导 合并
export const codeupGuideMergeActive = function (data) {
  return (dispatch) => {
    dispatch({
      type: CODEUP_GUIDE_MERGE,
      data,
    });
  };
};

// 获取库同步状态
export function getLastMirrorSyncRecord(clear = null) {
  return (dispatch, getState) => {
    const pageInfo = (getState().root && getState().root.pageInfo) || {};
    const pathResource = pageInfo.pathResource || {};
    exceed
      .fetch({
        api: 'project.mirror.last_mirror_sync_record',
        params: {
          projectId: pathResource.id,
        },
      })
      .then((result) => {
        if (result.sync_status !== 'READY' && result.sync_status !== 'SYNCHRONIZING') {
          clear && clear();
        }
        dispatch({
          type: PROJECT_RECEIVE_LAST_MIRROR_RECORD,
          data: result,
        });
      })
      .catch((error) => {
        console.error(error);
      });
  };
}

// 查询仓库同步设置
export function getMirrorSyncSettings() {
  return (dispatch, getState) => {
    const pageInfo = (getState().root && getState().root.pageInfo) || {};
    const pathResource = pageInfo.pathResource || {};
    exceed
      .fetch({
        api: 'project.mirror.settings',
        params: {
          projectId: pathResource.id,
        },
      })
      .then((result) => {
        dispatch({
          type: PROJECT_RECEIVE_MiRROR_SETTINGS,
          data: result,
        });
      });
  };
}

// 查询水印图片信息
export function genwatermark() {
  return (dispatch) => {
    exceed.fetch({ api: 'organization_setting_risk_gen_watermark' }).then((result) => {
      dispatch({
        type: CODEUP_GEN_WATERMARK_BASE64,
        data: result,
      });
    });
  };
}
